﻿Imports DevExpress.Web.ASPxScheduler
Imports DevExpress.XtraScheduler
Imports SIAVU.Dominio

Partial Public Class ucPostas
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PeriodoSeleccion.SelectedDate = CDate(Me.Periodo + "/01")
        InicializarScheduler()
    End Sub

#Region "Constantes"

    Const cDestinoDiasLibresID = Integer.MaxValue - 1000
    Const cDestinoDiasSinProgramarID = Integer.MaxValue - 999

#End Region

#Region "Variables"

    Private _Periodo As String
    Private _Plazo As Plazo = Nothing
    Private _ListaActividades As IList(Of Actividad)

#End Region

#Region "Propiedades"

    Public Property Periodo() As String
        Get
            Return _Periodo
        End Get
        Set(ByVal value As String)
            _Periodo = value
        End Set
    End Property

    Public Property Plazo() As Plazo
        Get
            If _Plazo Is Nothing Then
                Dim oPlazo As New Plazo
                oPlazo.getByFecha(CDate(Me.Periodo + "/01"))
                _Plazo = oPlazo
            End If
            Return _Plazo
        End Get
        Set(ByVal value As Plazo)
            _Plazo = value
        End Set
    End Property

    Public Property ListaActividades() As IList(Of Actividad)
        Get
            Return _ListaActividades
        End Get
        Set(ByVal value As IList(Of Actividad))
            _ListaActividades = value
        End Set
    End Property

#End Region

#Region "Metodos"

    Private Sub InicializarScheduler()
        Try
            schedulerPostas.Start = CDate(Me.Plazo.Anio.ToString() + "/" + Me.Plazo.Mes.ToString() + "/01") '//Siempre empieza el primer día de cada mes
            schedulerPostas.TimelineView.IntervalCount = UIManager.CantidadDiasDelMes(Me.Periodo.Split("/")(1))

        Catch ex As Exception
            Throw
        End Try
    End Sub

    Public Function FindActividadByID(ByVal ActividadID As Long) As Actividad
        Try
            For Each oActAux As Actividad In Me.ListaActividades
                If oActAux.ActividadID = ActividadID Then
                    Return oActAux
                End If
            Next
        Catch ex As Exception

        End Try
        Return Nothing

    End Function

    Private Sub AgregarImagenFondoAppointment(ByRef oAppointment As Appointment, ByRef oTimeLineAppointmentViewInfo As DevExpress.Web.ASPxScheduler.Drawing.TimeLineAppointmentViewInfo)
        Select Case oAppointment.LabelId
            Case ETipoActividad.Posta
                If Me.Page.AppRelativeVirtualPath.ToUpper = "~/Presentacion/ConfirmarTripulantes.aspx".ToUpper Then
                    If oAppointment.Start = Date.Now.ToShortDateString Then
                        'Poner icono de acuerdo a ??????
                    ElseIf oAppointment.Start < Date.Now.ToShortDateString Then
                        'Poner Iconos Check!
                    Else
                        'Poner Iconos Not Check
                    End If
                    oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.ImageUrl = "~/Imagenes/BluePlane.png"
                    oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.HorizontalPosition = "left"
                    oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.VerticalPosition = "top"
                    oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.Repeat = DevExpress.Web.ASPxClasses.BackgroundImageRepeat.NoRepeat
                Else
                    oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.ImageUrl = "~/Imagenes/BluePlane.png"
                    oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.HorizontalPosition = "left"
                    oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.VerticalPosition = "top"
                    oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.Repeat = DevExpress.Web.ASPxClasses.BackgroundImageRepeat.NoRepeat
                End If
            Case ETipoActividad.DiaLibre
                oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.ImageUrl = "~/Imagenes/YellowSmile.png"
                oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.HorizontalPosition = "center"
                oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.VerticalPosition = "top"
                oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.Repeat = DevExpress.Web.ASPxClasses.BackgroundImageRepeat.NoRepeat
            Case ETipoActividad.DiaSinProgramar
                oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.ImageUrl = "~/Imagenes/GreenQuestion.jpg"
                oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.HorizontalPosition = "center"
                oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.VerticalPosition = "top"
                oTimeLineAppointmentViewInfo.AppointmentStyle.BackgroundImage.Repeat = DevExpress.Web.ASPxClasses.BackgroundImageRepeat.NoRepeat
        End Select
    End Sub

    Private Sub AgregarActividadesDiasLibresYSinProgramar()
        Try
            'Dim ts As TimeSpan = Me.Plazo.FechaHastaModificacion.Subtract(Me.Plazo.FechaDesdeModificacion)

            'TODO Obtener valor del ultimo ActividadID de la  lista oDestinos y sacar las constantes. Pero dudo q existan tantos destinos
            Dim iActividadID As Integer = Me.ListaActividades.Count
            For i As Integer = 0 To UIManager.CantidadDiasDelMes(Me.Plazo.Mes) - 1 'ts.Days - 1
                Dim oActividadDiaLibre As New ActividadDiaLibre
                oActividadDiaLibre.ActividadID = iActividadID + 1
                iActividadID += 1
                oActividadDiaLibre.FechaInicio = CDate(Me.Plazo.Anio.ToString() + "/" + Me.Plazo.Mes.ToString() + "/01").AddDays(i)
                oActividadDiaLibre.FechaFin = CDate(Me.Plazo.Anio.ToString() + "/" + Me.Plazo.Mes.ToString() + "/01").AddDays(i + 1).AddMinutes(-1)
                oActividadDiaLibre.CiudadDestidoID = cDestinoDiasLibresID
                oActividadDiaLibre.EstadoActividad = EEstadoActividad.Desconocido
                oActividadDiaLibre.InformacionCompleta = "Dia Libre"
                Me.ListaActividades.Add(oActividadDiaLibre)

                Dim oActividadDiaSinPromgramar As New ActividadDiaSinProgramar
                oActividadDiaSinPromgramar.ActividadID = iActividadID + 1
                iActividadID += 1
                oActividadDiaSinPromgramar.FechaInicio = CDate(Me.Plazo.Anio.ToString() + "/" + Me.Plazo.Mes.ToString() + "/01").AddDays(i)
                oActividadDiaSinPromgramar.FechaFin = CDate(Me.Plazo.Anio.ToString() + "/" + Me.Plazo.Mes.ToString() + "/01").AddDays(i + 1).AddMinutes(-1)
                oActividadDiaSinPromgramar.CiudadDestidoID = cDestinoDiasSinProgramarID
                oActividadDiaLibre.EstadoActividad = EEstadoActividad.Desconocido
                oActividadDiaSinPromgramar.InformacionCompleta = "Dia Sin Programar"
                Me.ListaActividades.Add(oActividadDiaSinPromgramar)
            Next
        Catch ex As Exception
            'TODO Pagina de Error
            Throw
        End Try
    End Sub

    Private Sub AgregarDestinosDiasLibresYSinProgramar(ByVal oDestinos As IList(Of Ciudad))
        Try
            'TODO Obtener valor del ultimo CiudadID de la  lista oDestinos y sacar las constantes. Pero dudo q existan tantos destinos
            Dim oDestinoDiasLibres As New Ciudad
            Dim oDestinoDiasSinProgramar As New Ciudad

            oDestinoDiasLibres.CiudadID = cDestinoDiasLibresID
            oDestinoDiasLibres.Descripcion = "Libres"
            oDestinos.Add(oDestinoDiasLibres)

            oDestinoDiasSinProgramar.CiudadID = cDestinoDiasSinProgramarID
            oDestinoDiasSinProgramar.Descripcion = "Sin Programacion"
            oDestinos.Add(oDestinoDiasSinProgramar)
        Catch ex As Exception
            'TODO Pagina de Error
            Throw
        End Try
    End Sub

    Public Function GetPostaIdSeleccionada()
        Dim oActividad As Actividad = Me.FindActividadByID(Internal.AppointmentIdHelper.GetAppointmentId(Me.schedulerPostas.SelectedAppointments(0)))
        Return DirectCast(oActividad, ActividadPosta).Posta.PostaID
    End Function

#End Region

#Region "Eventos"

    Protected Sub schedulerPostas_PreparePopupMenu(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxScheduler.PreparePopupMenuEventArgs) Handles schedulerPostas.PreparePopupMenu
        Dim oPopUpMenu As ASPxSchedulerPopupMenu = e.Menu
        If oPopUpMenu.Id.Equals(SchedulerMenuItemId.DefaultMenu) Then
            oPopUpMenu.Items.Clear()
        End If
    End Sub

    Protected Sub ASPxScheduler1_AppointmentFormShowing(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxScheduler.AppointmentFormEventArgs) Handles schedulerPostas.AppointmentFormShowing
        e.Cancel = True
        Try


            'Primero obtengo la posta seleccionada para poder obtener las fechas reales
            'Dim objPosta As New Posta
            'objPosta.PostaID = Internal.AppointmentIdHelper.GetAppointmentId(e.Appointment)
            'objPosta.GetById(objPosta)

            Dim oucMiProgramacion As SIAVU.Presentacion.ucMiProgramacion = Nothing
            Dim oActividad As Actividad = Me.FindActividadByID(Internal.AppointmentIdHelper.GetAppointmentId(e.Appointment))
            Select Case Me.Page.AppRelativeVirtualPath.ToUpper
                Case "~/Presentacion/ReProgramarTripulantes.aspx".ToUpper
                    oucMiProgramacion = DirectCast(Page, SIAVU.Presentacion.ReprogramarTripulantes).ucMiProgramacion1
                    If oucMiProgramacion IsNot Nothing Then
                        oucMiProgramacion.InsertarNuevoActividad(oActividad)
                    End If

                Case "~/Presentacion/PreProgramacion.aspx".ToUpper
                    oucMiProgramacion = DirectCast(Page, SIAVU.Presentacion.PreProgramacion).ucMiProgramacion1
                    If oucMiProgramacion IsNot Nothing Then
                        oucMiProgramacion.InsertarNuevoActividad(oActividad)
                    End If

                Case "~/Presentacion/ConfirmarTripulantes.aspx".ToUpper
                    'Dim PostaID As Long = DirectCast(oActividad, ActividadPosta).Posta.PostaID
                    DirectCast(Page, SIAVU.Presentacion.ConfirmarTripulantes).CargarGrillaTripulantes()
            End Select



        Catch ex As Exception
            Throw
        End Try

    End Sub

    Protected Sub odsDestinos_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles odsDestinos.Selected
        If Me.Page.AppRelativeVirtualPath.ToUpper = "~/Presentacion/PreProgramacion.aspx".ToUpper Or Me.Page.AppRelativeVirtualPath.ToUpper = "~/Presentacion/ReprogramarTripulantes.aspx".ToUpper Then
            AgregarDestinosDiasLibresYSinProgramar(e.ReturnValue)
        End If
    End Sub

    Protected Sub odsPostas_Selected(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles odsActividades.Selected
        Try
            Me.ListaActividades = e.ReturnValue
            If Me.Page.AppRelativeVirtualPath.ToUpper = "~/Presentacion/PreProgramacion.aspx".ToUpper Or Me.Page.AppRelativeVirtualPath.ToUpper = "~/Presentacion/ReprogramarTripulantes.aspx".ToUpper Then
                AgregarActividadesDiasLibresYSinProgramar()
            End If
        Catch ex As Exception
            'TODO Pagina de Error
            Throw
        End Try
    End Sub

    Protected Sub schedulerPostas_InitAppointmentImages(ByVal sender As Object, ByVal e As DevExpress.Web.ASPxScheduler.AppointmentImagesEventArgs) Handles schedulerPostas.InitAppointmentImages
        Try
            Dim oAppointment As Appointment = e.Appointment
            Dim oTimeLineAppointmentViewInfo As DevExpress.Web.ASPxScheduler.Drawing.TimeLineAppointmentViewInfo = DirectCast(e.ViewInfo, DevExpress.Web.ASPxScheduler.Drawing.TimeLineAppointmentViewInfo)
            Dim oAppointmentImageInfoCollection As DevExpress.Web.ASPxScheduler.Drawing.AppointmentImageInfoCollection = e.ImageInfoList

            AgregarImagenFondoAppointment(oAppointment, oTimeLineAppointmentViewInfo)
            'AgregarImagenSegunEstadoAppointment(oAppointment, oAppointmentImageInfoCollection)

        Catch ex As Exception
            Throw
        End Try
    End Sub

#End Region

    ' ''Private Sub AgregarImagenSegunEstadoAppointment(ByRef oAppointment As Appointment, ByRef oAppointmentImageInfoCollectionas As DevExpress.Web.ASPxScheduler.Drawing.AppointmentImageInfoCollection)
    ' ''    Select Case oAppointment.StatusId
    ' ''        Case EEstadoActividad.Confirmada
    ' ''            Dim imageproperties As New DevExpress.Web.ASPxClasses.ImageProperties("~/imagenes/squaregreencheck.png")
    ' ''            imageproperties.Width = Unit.Pixel(15)
    ' ''            imageproperties.Height = Unit.Pixel(15)
    ' ''            Dim info As New DevExpress.Web.ASPxScheduler.Drawing.AppointmentImageInfo()
    ' ''            info.ImageProperties = imageproperties
    ' ''            oAppointmentImageInfoCollectionas.Add(info)

    ' ''        Case EEstadoActividad.NoConfirmada

    ' ''    End Select
    ' ''End Sub
End Class